import java.net.InetSocketAddress;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import jane.core.HttpCodec;
import jane.core.NetManager;
import jane.core.Octets;
import jane.core.OctetsStream;
import jane.tool.CachedIoBufferAllocator;

public class BenchJane extends NetManager {
	private static final int PORT = Integer.getInteger("port", 10000);
	private final Octets helloBody = Octets.wrap("Hello, World!");
	private final Octets extraHead;

	public BenchJane() {
		IoBuffer.setUseDirectBuffer(Boolean.getBoolean("direct"));
		if (Boolean.getBoolean("pool"))
			IoBuffer.setAllocator(new CachedIoBufferAllocator());
		extraHead = HttpCodec.createExtraHead("Content-Type: text/plain; charset=UTF-8", "Server: jane0");
		setIoThreadCount(Integer.getInteger("thread", 1));
		setCodecFactory(HttpCodec::new);
		getAcceptor().getSessionConfig().setTcpNoDelay(true);
	}

	@Override
	public void messageReceived(IoSession session, Object message) {
		OctetsStream os = (OctetsStream)message;
		if (HttpCodec.getHeadPath(os).equals("/plaintext"))
			HttpCodec.sendHead(session, null, helloBody.size(), extraHead, helloBody);
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) {
	}

	public static void main(String[] args) {
		new BenchJane().startServer(new InetSocketAddress(PORT));
	}
}
